/**
 * WeeShop 声明
 * ===========================================================
 * 网站： https://www.darlang.com
 * 标题： ECShop 小程序「weeshop 」- 基于 ECShop 为后台系统开发的非官方微信商城小程序
 * 链接： https://www.darlang.com/?p=709
 * 说明： 源码已开源并遵循 Apache 2.0 协议，你有权利进行任何修改，但请保留出处，请不要删除该注释。
 * ==========================================================
 * Copyright 2019 darlang
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ===========================================================
 */
// cart.js
import utils from '../../../utils/utils.js';
import {PNT,setNavBarTitle,showToast,pushPagePath,formatTime} from "../../../utils/utils";
import { DeleteCartGoods, GetCartGoods, UpdateCartGoods, updateCartGoodsActivity, UpSelectGoods} from "../../../utils/apis";
import {CheckCartTotal} from "../../../utils/publics";

Page({
  /**
   * 页面的初始数据
   */
  data: {
    cartGoodsLst: '',
    cartTotal: {
      "count": 0,
      "amount": 0.00
    },
    cartSeleStatus: '',
    cartEditStatus: false,
    show:false,
    act_id:0,
    activity:[],
    rec_id:0,
    i:0,
    ii:0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function () {
    setNavBarTitle(PNT.goods.cart);
    this.loginModal = this.selectComponent("#login-modal");
  },

  /**
   * 页面跳转
   * @author darlang
   */
  pushPath(e) {
    const items = e.currentTarget.dataset;
    const pathData = [
      {type: 'goods',path: '/pages/goods/detail/detail?id='+items.id},
      {type: 'list',path: '/pages/goods/list/list'}
    ];
    pushPagePath(e,pathData);
  },

  /**
   * 隐藏
   * @author darlang
   */
  hide(e){
    this.setData({
      show:false,
    })
  },
  /**
 * 显示折扣
 * @author darlang
 */
  show(e) {
    var items = e.currentTarget.dataset;
    var activity = this.data.cartGoodsLst;
    let i,ii;
    i = items.i;ii = items.ii;
    this.setData({
      act_id: items.id,
      show:true,
      ii:items.ii,
      i:items.i,
      rec_id:items.rec,
      activity:activity[ii].goods[i].activity,
    })
  },
/**
 * 切换折扣
 * @author darlang
*/
  changeActivity(e){
    var items = e.currentTarget.dataset;
    var act_id = items.act;
    if(act_id == this.data.act_id){
      return;
    }
    var data = {};
    var info = wx.getStorageSync('userInfo');
    data.user_id = info.user_id;
    data.rec_id = this.data.rec_id;
    data.act_id = act_id;
    var list = this.data.cartGoodsLst;


    // var index = e.currentTarget.dataset.index;
    // var activity = this.data.activity;
    // for(var i = 0;i<activity.length;i++){
    //   if(i == index){
    //     activity[i].selected = true;
    //   }else{
    //     activity[i].selected = false;
    //   }
    // }


    // console.log(list)
    // this.setData({
    //   activity:activity,
    //   cartGoodsLst:list,
    //   show:false,
    // })
    updateCartGoodsActivity(data).then(res=>{
      if(res.error_code == 10000){
        list[this.data.ii].goods[this.data.i].act_id = act_id;
        this.setData({
          cartGoodsLst:list,
          act_id:act_id,
          show:false,
        })
      }
    })
  },

  /**
   * 登录回调
   * @author darlang
   */
  loginCallback(cb) {
    if (cb.detail.type === 'success') {
      this.getCartList();
      var info = wx.getStorageSync('userInfo')
      this.setData({
        info:info,
      })
      // CheckCartTotal();
    }
  },

  /**
   * 移出购物车
   * @author darlang
   */
  delCartGoods(e) {
    let self = this;
    let i = e.target.dataset.i;
    let cartGoodsLst = this.data.cartGoodsLst;
    var info = wx.getStorageSync('userInfo');
    wx.showModal({
      title: '温馨提示',
      content: '是否将选中商品移出购物车？',
      success: function (res) {
        if (res.confirm) {
          wx.showLoading({title: '移除中...',mask: true});
          DeleteCartGoods(cartGoodsLst[i].rec_id,info.user_id).then(res => {
            if(res.error_code == 10000){
              utils.showToast('成功', 'success');
              cartGoodsLst.splice(i, 1);
              self.setData({
                cartGoodsLst: cartGoodsLst
              });
            }else{
              utils.showToast('失败', 'fail');
            }

            // CheckCartTotal();
          });
        }
      }
    });
  },

  /**
   * 获取购物车商品列表
   * @author darlang
   */
  getCartList() {
    wx.showLoading({title: '加载中...',mask: true});
    var info = wx.getStorageSync('userInfo');
    console.log(info)
    GetCartGoods(info.user_id).then(res => {
      console.log(res)
      if(res.error_code == 10000){
        this.setData({
          cartGoodsLst: res.data
        })
        this.checkedSeleGoods();
      }
      return;
      // if (res.goods_groups && res.goods_groups.length > 0) {
      //   let cartGoodsList = res.goods_groups[0].goods;
      //   this.setData({
      //     cartGoodsLst: cartGoodsList
      //   });
      //   this.checkCartSeleState(cartGoodsList);
      // }
      // 
    });
    // CheckCartTotal();
  },

  /**
   * 选择检查
   * @author darlang
   * @param  {Array}   data  购物车商品
   * @return {String}        ''、some、all
   */
  checkCartSeleState(data) {
    let seleState = '';
    let cartGoodsLst = data;
    seleState = cartGoodsLst.every(k => k.goods.every(kk=>kk.is_checked));
    if (!seleState) {
      seleState = cartGoodsLst.some(k =>k.goods.some(kk=>kk.is_checked));
      if (seleState) {
        seleState = 'some';
      }else{
        seleState = '';
      }
    }else{
      seleState = 'all';
    }
    this.setData({
      cartSeleStatus: seleState
    });
    return seleState;
  },

  /**
   * 统计已选价格、数量
   * @author darlang
   * @return {Object}   返回选择数量及总额
   */
  checkedSeleGoods() {
    let cartGoodsAmount = 0;
    let cartGoodsCount = 0;
    let cartGoodsLst = this.data.cartGoodsLst;

    if (cartGoodsLst && cartGoodsLst.length > 0) {
      for (let ii = 0; ii < cartGoodsLst.length; ii++){
      for (let i = 0; i < cartGoodsLst[ii].goods.length; i++) {
        if (cartGoodsLst[ii].goods[i].is_checked) {
          cartGoodsAmount = Number(cartGoodsAmount + (cartGoodsLst[ii].goods[i].goods_price * cartGoodsLst[ii].goods[i].goods_number));
          // if (cartGoodsLst[i].activity.length > 0){
          //   for (var a = 0; a < cartGoodsLst[i].activity.length;a++){
          //     if (cartGoodsLst[i].activity[a].selected){
          //       cartGoodsLst[i]
          //     }
          //   }
          // }else{
            cartGoodsCount += parseInt(cartGoodsLst[ii].goods[i].goods_number);
          // }
        }
      }
      }
    }
    if (isNaN(cartGoodsAmount)) {
      cartGoodsAmount = 0;
    }
    if (isNaN(cartGoodsCount)) {
      cartGoodsCount = 0;
    }
    this.setData({
      'cartTotal.count': cartGoodsCount,
      'cartTotal.amount': cartGoodsAmount.toFixed(2)
    });
    // var arr = [1, 2, 3, 4, 5, 6];

    // console.log(arr.some(function (item, index, array) {
    //   console.log('item=' + item + ',index=' + index + ',array=' + array);
    //   return item > 3;
    // }));
    return {amount: cartGoodsAmount,count: cartGoodsCount};
  },

  /**
   * 选择当前项
   * @author darlang
   */
  seleGoods(e) {
    let i = e.currentTarget.dataset.i;
    let ii = e.currentTarget.dataset.ii;
    console.log(ii)
    let g = this.data.cartGoodsLst;
    if (g[ii].goods[i]) {
      console.log(g[ii].goods[i].is_checked)
      if (g[ii].goods[i].is_checked) {
        g[ii].goods[i].is_checked = 0;
      }else{
        g[ii].goods[i].is_checked = 1;
      }
    }
    this.setData({
      cartGoodsLst: g
    });
    var data = {};
    data.user_id = this.data.info.user_id;
    data.rec_id = g[ii].goods[i].rec_id;
    data.status = g[ii].goods[i].is_checked;
    this.upSelectGoods(data);
    this.checkCartSeleState(g);
    this.checkedSeleGoods();
  },
  /**
   * 更新当前选项
   * @author darlang
   */
  upSelectGoods(data){
    UpSelectGoods(data).then(res=>{
      if(res.error_code == 10000){
        console.log(data)
      }
    })
  },

  /**
   * 全选/取消全选
   * @author darlang
   */
  seleAllGoods() {
    let cartGoodsLst = this.data.cartGoodsLst;
    if (cartGoodsLst && cartGoodsLst.length) {
      if (cartGoodsLst.every(k => k.goods.every(kk=>kk.is_checked))) {
        cartGoodsLst = cartGoodsLst.map((k) => {
          k.goods.map(kk=>{
            kk.is_checked = false;
            return kk;
          })
          return k;
        });
      }else{
        cartGoodsLst = cartGoodsLst.map((k) => {
          k.goods.map(kk => {
            kk.is_checked = true;
            return kk;
          })
          return k;
        });
      }

    }
    this.setData({
      cartGoodsLst: cartGoodsLst
    });
    this.checkCartSeleState(cartGoodsLst);
    this.checkedSeleGoods();
  },

  /**
   * 修改购物车商品数量
   * @author darlang
   */
  changeCartGoodsAmount(e) {
    const g = this.data.cartGoodsLst;
    const items = e.currentTarget.dataset;
    const type = items.type;
    const i = items.i;
    const ii = items.ii;
    const stock = g[ii].goods[i].attr_stock;
    const rec_id = items.rec;
    let num = parseInt(items.num) || '1';

    if (type === 'add') {
      num++;
    }
    if (type === 'cut') {
      num--;
    }
    if (type === 'input') {
      num = e.detail.value;
    }
    if (num >= stock && stock >= 1) {
      num = stock;
    }else if(num <= 1) {
      num = 1;
    }
    num = stock === 0 ? 0 : num;
    g[ii].goods[i].goods_number = num;

    this.updateCartGoods(rec_id,num,g);
  },

  /**
   * 更新购物车商品
   * @author darlang
   * @param  {Number}   goodsId   商品id
   * @param  {Number}   amount 数量
   */
  updateCartGoods(goodsId, amount,g) {
    var info = wx.getStorageSync('userInfo');
    var data = {};
    data.user_id = info.user_id;
    data.amount = amount;
    data.good = goodsId;
    UpdateCartGoods(data).then((res) => {
      
      // CheckCartTotal();
      this.setData({
        cartGoodsLst: g
      });
    });
  },

  /**
   * 提交前检查数量，防止超出库存
   * @author darlang
   * @param  {Array}   g 商品列表
   */
  checkSeleItemStock(g) {
    if (g && g.length > 0) {
      for (let i = 0; i < g.length; i++) {
        if (g[i].checked) {
          g[i].amount = g[i].amount >= g[i].attr_stock ? g[i].attr_stock : g[i].amount;
        }
      }
    }

    return g;
  },
  /**
   * 计算价格
   * @author darlang
   */
  checkTotalPrice(){
    var list = this.data.cartGoodsLst;

  },


  /**
   * 提交检查订单
   * @author darlang
   */
  checkoutGoods() {
    let self = this;
    const g = this.checkSeleItemStock(this.data.cartGoodsLst);
    const checkedGoods = g.filter(k => k.goods.filter(kk=>kk.is_checked));
    if (!checkedGoods || checkedGoods.length === 0) {
      utils.showToast('未选择商品','warning');
      return false;
    }
    let preOrder = [];
    let orderInfo = [];
    // checkedGoods.map(k => {
    //   let attrs = k.attrs.split(',');
    //   let props = k.product.properties;
    //   let attrsInfo = [];

      preOrder = {
        // id: k.id,
        // "goods_id": k.goods_id,
        // "property": k.attrs.split(),
        // "num": k.amount,
        // "total_amount": k.amount
        type:'cart',
        user_id:this.data.info.user_id
      };

    //   attrsInfo = self.checkGoodsAttrs(attrs,props);

    //   orderInfo.push({
    //     "name": k.product.name,
    //     "id": k.goods_id,
    //     "price": k.price,
    //     "total": k.amount,
    //     "thumb": k.product.default_photo.large || '/images/default_image.png',
    //     "score": k.product.score || 0,
    //     "attrs": attrsInfo
    //   });
    // });
    // orderInfo = escape(JSON.stringify(orderInfo));
    // preOrder = [{'type':'cart'}];
    preOrder = JSON.stringify(preOrder);
    wx.navigateTo({
      url: '/pages/shopping/checkout/checkout?type=cart&preOrder='+preOrder
    });
  },

  /**
   * 对齐选择商品规格
   * @author darlang
   * @param  {Array}   attrs 属性
   * @param  {Array}   props 规格
   * @return {Array}         返回选择的规格属性
   */
  checkGoodsAttrs(attrs,props) {
    // console.time();
    let attrsInfo = [];
    if (attrs && attrs.length > 0) {
      for (let i = 0; i < props.length; i++) {
        if (props[i].attrs && props[i].attrs.length > 0) {
          for (let j = 0; j < props[i].attrs.length; j++) {
            if (attrs.includes((props[i].attrs[j].id.toString()))) {
              props[i].attrs[j].attr_name = props[i].name+':'+props[i].attrs[j].attr_name;
              attrsInfo.push(props[i].attrs[j]);
            }
          }
        }
      }
      attrsInfo = attrsInfo.map(k => {
        return {
          name: k.attr_name,
          price: k.attr_price,
          id: k.id
        };
      });
      attrsInfo.sort((a,b) => a.id - b.id);
    }
    // console.timeEnd();
    return attrsInfo;
  },



  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    // 页面渲染完成
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // 页面显示
    
    if (!this.loginModal.check()) {
      return false;
    }
    this.getCartList();
    var info = wx.getStorageSync('userInfo')
    this.setData({
      info:info
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    // 页面隐藏

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    // 页面关闭

  },
});